﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibrarySystem
{
    public partial class DeleteOfBill : Form
    {
        AppManager LIB = new AppManager();
        public DeleteOfBill()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                label2.Text = "";
                int number = 0;
                if (int.TryParse(textBox1.Text, out number))
                {
                    label2.Text = "";
                    LIB.GridDisplay(dataGridView1, "Bill", "select * from Bill where رقم_الفاتورة = " + textBox1.Text + "");
                    if (dataGridView1.RowCount > 1)
                    {
                        label2.Text = "";
                        LIB.ComboDisplayChoose(comboBox1, "select * from Bill where رقم_الفاتورة = " + textBox1.Text + "", "Bill", "عدد_الأصناف", textBox2);
                        if (textBox2.Text != "")
                        {
                            label2.Text = "";
                            int I;    
                            I = int.Parse(comboBox1.Text);
                            LIB.GridDisplay(dataGridView1, "Invoice_Details", "select * from Invoice_Details where رقم_الفاتورة = " + textBox1.Text + "");
                            for (int i = 0; i < I; i++)
                            {
                                textBox3.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
                                LIB.ComboDisplay(valuenew, "select * from Invoice_Details where رقم_الفاتورة = " + textBox1.Text + " AND كود_الكتاب = '" + textBox3.Text + "' ", "Invoice_Details", "الكمية");
                                LIB.ComboDisplay(valueold, "select * from BOOKS where كود_الكتاب = '" + textBox3.Text + "'", "BOOKS", "اجمالي_النسخ");
                                total.Text = Convert.ToString((int.Parse(valuenew.Text)) + (int.Parse(valueold.Text)));
                                LIB.update("UPDATE BOOKS SET اجمالي_النسخ=" + total.Text + " where كود_الكتاب='" + textBox3.Text + "'");
                                    
                            }
                            LIB.delete("DELETE from Bill  where رقم_الفاتورة = " + textBox1.Text + " ");
                            label2.Text = "تم الحذف";
                        }
                        else
                        {
                            LIB.delete("DELETE from Bill  where رقم_الفاتورة = " + textBox1.Text + " ");
                            label2.Text = "تم الحذف";
                        }
                    }
                    else
                        label2.Text = "رقم الفاتورة غير موجود";
                }
                else
                    label2.Text = "قم بإدخال رقم صحيح للفاتورة";

            }
            else
                label2.Text = "قم بإدخال رقم الفاتورة";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void valuenew_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void valueold_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void total_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label2.Text = "";
        }

        private void DeleteOfBill_Load(object sender, EventArgs e)
        {

        }
    }
}
